const screenitemservice = require("../../../../apiservice/screen/screenitemservice.js");
const app = getApp();
const regeneratorRuntime = getApp().globalData.regeneratorRuntime;
const util = require("../../../../utils/util")
module.exports = Behavior({
	behaviors: [],
	properties: {},
	data: {},
	methods: {
		initData:function(options){
			this.setData({
        screenId:options.new_screenId,
				appointmenttothehospitaldate: util.frmatDate(new Date()),
				//screenitemIndex:this.getPickerValue(this.data.screenitem,'new_screenitemId',options.new_screenteamId),
				['reservation.new_reservationdate']: util.frmatDate(new Date()),
				['leadEntity.new_inputingtime']: util.frmatDate(new Date()),
				['nakedEyeRightRes.new_scid']: options.new_screenId,
				['nakedEyeLeftRes.new_scid']: options.new_screenId,
				['nakedEyeRight.new_scid']: options.new_screenId,
				['nakedEyeLeft.new_scid']: options.new_screenId,
				['eyeResult.new_scid']: options.new_screenId,
				['correctRight.new_scid']: options.new_screenId,
				['correctLeft.new_scid']: options.new_screenId,
				['cataractRight.new_scid']: options.new_screenId,
				['cataractLeft.new_scid']: options.new_screenId,
				['pterygiumRight.new_scid']: options.new_screenId,
				['pterygiumLeft.new_scid']: options.new_screenId,
				['lacrimalObstruction.new_scid']: options.new_screenId,
				['trichiasis.new_scid']: options.new_screenId,
				['conjunctivitis.new_scid']: options.new_screenId,
				['ametropia.new_scid']: options.new_screenId,
				['fundusDiseases.new_scid']: options.new_screenId,
				['descriptions.new_scid']: options.new_screenId,
				['advise.new_scid']: options.new_screenId,
				['reservation.new_screen']: options.new_screenId,
				['propageanda.new_screen']: options.new_screenId,
				['sccustomer.new_scid']: options.new_screenId,
				['systemicDiseases.new_scid']: options.new_screenId,
				//
				['nakedEyeRightRes.new_doctorid']: options.new_doctorid,
				['nakedEyeLeftRes.new_doctorid']: options.new_doctorid,
				['eyeResult.new_doctorid']: options.new_doctorid,
				['nakedEyeRight.new_doctorid']: options.new_doctorid,
				['nakedEyeLeft.new_doctorid']: options.new_doctorid,
				['correctRight.new_doctorid']: options.new_doctorid,
				['correctLeft.new_doctorid']: options.new_doctorid,
				['cataractRight.new_doctorid']: options.new_doctorid,
				['cataractLeft.new_doctorid']: options.new_doctorid,
				['pterygiumRight.new_doctorid']: options.new_doctorid,
				['pterygiumLeft.new_doctorid']: options.new_doctorid,
				['lacrimalObstruction.new_doctorid']: options.new_doctorid,
				['trichiasis.new_doctorid']: options.new_doctorid,
				['conjunctivitis.new_doctorid']: options.new_doctorid,
				['ametropia.new_doctorid']: options.new_doctorid,
				['fundusDiseases.new_doctorid']: options.new_doctorid,
				['descriptions.new_doctorid']: options.new_doctorid,
				['advise.new_doctorid']: options.new_doctorid,
				['propageanda.new_doctor']: options.new_doctorid,
				['systemicDiseases.new_doctorid']: options.new_doctorid,
				//
				['nakedEyeRightRes.ownerid']: options.new_doctorid,
				['nakedEyeLeftRes.ownerid']: options.new_doctorid,
				['eyeResult.ownerid']: options.new_doctorid,
				['nakedEyeRight.ownerid']: options.new_doctorid,
				['nakedEyeLeft.ownerid']: options.new_doctorid,
				['correctRight.ownerid']: options.new_doctorid,
				['correctLeft.ownerid']: options.new_doctorid,
				['cataractRight.ownerid']: options.new_doctorid,
				['cataractLeft.ownerid']: options.new_doctorid,
				['pterygiumRight.ownerid']: options.new_doctorid,
				['pterygiumLeft.ownerid']: options.new_doctorid,
				['lacrimalObstruction.ownerid']: options.new_doctorid,
				['trichiasis.ownerid']: options.new_doctorid,
				['conjunctivitis.ownerid']: options.new_doctorid,
				['ametropia.ownerid']: options.new_doctorid,
				['fundusDiseases.ownerid']: options.new_doctorid,
				['descriptions.ownerid']: options.new_doctorid,
				['advise.ownerid']: options.new_doctorid,
				//['reservation.ownerid']: options.new_doctorid,
				['propageanda.ownerid']: options.new_doctorid,
				['sccustomer.ownerid']: options.new_doctorid,
				['leadEntity.ownerid']: options.new_doctorid,
				['systemicDiseases.ownerid']: options.new_doctorid,
				['propageanda.new_followdoctor']: options.new_doctorid,
				//
				['sccustomer.new_screenteamid']: options.new_screenteamId,
			});
		},
		//获取所有筛查结果
		getALLscResult: function() {
			var result = '';
			if (this.data.result.length > 0) {
				this.data.result.forEach(item => {
					if (item == '1') {
						result += 'SC0000000096' + ','
					} else if (item == 2) {
						result += 'SC0000000097' + ','
					} else if (item == 3) {
						result += 'SC0000000098' + ','
					} else if (item == 4) {
						result += 'SC0000000099' + ','
					}
				});
			}
			var total = [];
			var stage = this.data.stage;
			if (result) {
				var systemicDiseases = this.data.systemicDiseases; //筛查结果 全身疾病史
				systemicDiseases.new_result = result;
				total.push(systemicDiseases);
			}
			if (this.data.nakedEyeRightId != 0) {
				total.push(this.data.nakedEyeRight);
			}
			if (this.data.nakedEyeLeftId != 0) {
				total.push(this.data.nakedEyeLeft);
			}
			if (this.data.correctRightId != 0) {
				total.push(this.data.correctRight);
			}
			if (this.data.correctLeftId != 0) {
				total.push(this.data.correctLeft);
			}

      // 社区、大客户、大学校园筛查不需要判断是否正常；而中小学校园没有单人模式，所以单人模式不需要判断正、异常
			// if(this.data.nakedEyeLeft.new_result=='无光感'||this.data.nakedEyeLeft.new_result=='手动'||this.data.nakedEyeLeft.new_result=='指数'
			// ||this.data.nakedEyeRight.new_result=='光感'||this.data.nakedEyeLeft.new_result=='光感'
			// ||this.data.nakedEyeRight.new_result=='无光感'||this.data.nakedEyeRight.new_result=='手动'||this.data.nakedEyeRight.new_result=='指数'){
			// 	this.data.eyeResult.new_result = 'SC0000000005';
			// }else{
			// 	var signLeft = parseFloat(this.data.nakedEyeLeft.new_result);
			// 	var signRight = parseFloat(this.data.nakedEyeRight.new_result);
			// 	if(stage!=''&&stage=='1'){
			// 		if (signLeft < 4.8 || signLeft > 4.9||signRight < 4.8 || signRight > 4.9) {
			// 			this.data.eyeResult.new_result = 'SC0000000005';
			// 		}
			// 	}else{
			// 		if (signLeft < 4.9 || signRight < 4.9) {
			// 			this.data.eyeResult.new_result = 'SC0000000005';
			// 		}
			// 	}	
			// }
      //total.push(this.data.eyeResult);
      //单人模式 结果判断结束

			total.push(this.data.nakedEyeRightRes);
			total.push(this.data.nakedEyeLeftRes);
			total.push(this.data.cataractRight);
			total.push(this.data.cataractLeft);
			total.push(this.data.pterygiumRight);
			total.push(this.data.pterygiumLeft);
			total.push(this.data.lacrimalObstruction);
			total.push(this.data.trichiasis);
			total.push(this.data.conjunctivitis);
			total.push(this.data.ametropia);
			total.push(this.data.fundusDiseases);
			
			if (this.data.descriptions.new_result.replace(/\s+/g, '') != '') {
				total.push(this.data.descriptions)
			}
			if (this.data.advise.new_result.replace(/\s+/g, '') != '') {
				total.push(this.data.advise)
			}
			return total
		},
		
		//创建社区筛查单人模式录入
		CreateSQSC: async function(obj) {
			var that = this;
			//var result = {};

			var result = await screenitemservice.CreateSQSC(obj);
      
			this.defaultValue(result.data);
			wx.hideLoading();
			this.showResult(result.data);
		},
		//获取所有医院
		GetAllHospital: function() {
			var that = this;
			screenitemservice.GetAllHospital().then(function(success) {
				that.setData({
					hospitalList: success.data
				});
			}).catch(function(ex) {
				console.log(ex);
			})
		},
		getSchoolListByType: function(screenitemId) {
			if(!screenitemId) return;
			var that = this;
			//var defaultlist=[{new_name:'全部',new_schoolid:null}];
			screenitemservice.GetScreenAllTeams().then(function(success) {
				//console.log(success.data);       
				var list = that.data.screenitem.concat(success.data);	
				
				var screenitemIndex =that.getPickerValue(list,'new_screenitemId',screenitemId);	
				var new_screenitemId = list[screenitemIndex].new_screenitemId;			
				that.setData({
					screenitem: list,
					screenitemIndex: screenitemIndex,
					['leadEntity.new_screenitemId'] : new_screenitemId,
					['sccustomer.new_scitemid']: new_screenitemId,
					['reservation.new_new_screenitem']:new_screenitemId,
				});
			}).catch(function(ex) {
				console.log(ex);
			})
		},
		GetDepartmentsByHospID: function(hospitalid) {
			var that = this;
			//var defaultlist=[{new_name:'全部',new_schoolid:null}];
			screenitemservice.GetDepartmentsByHospID(hospitalid).then(function(success) {
				//console.log(success.data);     
				var result = success.data;
				if (result.length >= 1 && result[0].new_departmentId == undefined) {
					result = [];
				} else {
					//debugger
					var officeIndex = that.data.officeIndex;
					var new_department = result[officeIndex].new_departmentId
					that.setData({
						['reservation.new_department']: new_department
					});
				}
				that.setData({
					officeArray: result
				});
			}).catch(function(ex) {
				console.log(ex);
			})
		},
		//获取下标
		/**
		 * 
		 *
		 * @param {*} targetObj 目标对象
		 * @param {*} id  目标ID
		 * @returns 目标ID的下标
		 */
		getPickerValue: function(targetObj,targetObjName, id) {
			for (let index in targetObj) {
				if (targetObj[index][targetObjName] == id) {
					return index
				}
			}
		},
		ages: function(str) {
			var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
			if (r == null) return false;
			var d = new Date(r[1], r[3] - 1, r[4]);
			if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
				var Y = new Date().getFullYear();
				return (Y - r[1]);
			}
			return ("输入的日期格式错误！");
		},
		showResult: function(res) {
			var title = '';
			var icon = '';
			if (res.resultState == 1) {
				title = '创建成功';
				icon = 'success';
			} else {
				title = '创建失败';
				icon = 'none';
			}
			wx.showToast({
				title: title,
				icon: icon,
				duration: 2000
			});
    }, 
		//提交表单后恢复默认值
		defaultValue: function (res) {
			if (res.resultState == 1) {
				this.setData({
          IsShowtip:false,
					active1: 1,
					active0: 0,
					value: '',
					age: '',
					//手机号码
					phone: '',
					//手机号码2
					phone2: '',
					//固话
					tel: '',
					//固话2
					tel2: '',
					['idCardInfo.name']: '',
					['idCardInfo.sex']: 1,
					//screenitemIndex: 0,
					['idCardInfo.id']: '',
					['idCardInfo.homePlace']: '',
          ['idCardInfo.address.text']:'',
          ['idCardInfo.sex.text']:"男",
					nakedEyeRightText: '请填写',
					['nakedEyeRight.new_result']: '',
					nakedEyeLeftText: '请填写',
					['nakedEyeLeft.new_result']: '',
					correctRightText: '请选择',
					['correctRight.new_result']: '',
					correctLeftText: '请选择',
					['correctLeft.new_result']: '',
					result: [],
					reservationShow: false,
					iSreservation: false,
					appointmenttothehospitaldate: util.frmatDate(new Date()),
					advisevalue: '',
					['advise.new_result']: '',
					['leadEntity.description']: '',
					['leadEntity.new_gender']: 1,
					['leadEntity.new_inputingtime']: '',
					['leadEntity.new_patientsource']: 3,
					['leadEntity.new_datasource']: 17,
					['leadEntity.lastname']: '',
          ['leadEntity.new_age']:0,
          ['leadEntity.address1_telephone1']:'',
          ['leadEntity.new_mobile']:'',
          ['leadEntity.new_citizenid']:'',
					//预约
					['reservation.new_source']: 3,
					['reservation.new_mobile']: '',
					['reservation.new_gender']: 1,
					['reservation.new_insurancetype']: '',
					['reservation.new_reservationdate']: '',
					['reservation.new_hospital']: '',
					['reservation.new_department']: '',
					['reservation.new_type']: 3,
					['reservation.new_contnt']: '',
          ['reservation.new_age']: '',
					//宣教
					['propageanda.new_gender']: 1,
					['propageanda.new_age']: '',
					['propageanda.new_mobile']: '',
					['propageanda.new_tel']: '',
					['propageanda.new_healthinsuranceplace']: '',
					['propageanda.new_isorder']: false,
					['propageanda.new_isfollowself']: 2,
					['propageanda.new_countteardrop']: '',

					//筛查客户
					['sccustomer.new_scstate']: 1,
					['new_scnum']: '',
					//右眼筛查结果  默认未术
					['nakedEyeRightRes.new_categoryid']: 'LB0007',
					['nakedEyeRightRes.new_itemid']: 'XM0014',
					['nakedEyeRightRes.new_contentid']: 'NR0160',
					['nakedEyeRightRes.new_result']: 'SC0000000094',

					//左裸眼筛查结果  默认未术
					['nakedEyeLeftRes.new_categoryid']: 'LB0007',
					['nakedEyeLeftRes.new_itemid']: 'XM0014',
					['nakedEyeLeftRes.new_contentid']: 'NR0161',
					['nakedEyeLeftRes.new_result']: 'SC0000000093',
					//裸眼视力 右
					['nakedEyeRight.new_categoryid']: 'LB0007',
					['nakedEyeRight.new_itemid']: 'XM0014',
					['nakedEyeRight.new_contentid']: 'NR0023',
					['nakedEyeRight.new_result']: '',
					//裸眼视力 左
					['nakedEyeLeft.new_categoryid']: 'LB0007',
					['nakedEyeLeft.new_itemid']: 'XM0014',
					['nakedEyeLeft.new_contentid']: 'NR0025',
					['nakedEyeLeft.new_result']: '',
					//矫正视力 右
					['correctRight.new_categoryid']: 'LB0007',
					['correctRight.new_itemid']: 'XM0010',
					['correctRight.new_contentid']: 'NR0024',
					['correctRight.new_result']: '',
					//矫正视力 左
					['correctLeft.new_categoryid']: 'LB0007',
					['correctLeft.new_itemid']: 'XM0010',
					['correctLeft.new_contentid']: 'NR0026',
					['correctLeft.new_result']: '',
					//白内障 右
					['cataractRight.new_categoryid']: 'LB0001',
					['cataractRight.new_itemid']: 'XM0002',
					['cataractRight.new_contentid']: 'NR0165',
					['cataractRight.new_result']: 'SC0000000029',
					//白内障 左
					['cataractLeft.new_categoryid']: 'LB0001',
					['cataractLeft.new_itemid']: 'XM0002',
					['cataractLeft.new_contentid']: 'NR0166',
					['cataractLeft.new_result']: 'SC0000000029',

					//翼状胬肉(右眼)
					['pterygiumRight.new_categoryid']: 'LB0002',
					['pterygiumRight.new_itemid']: 'XM0003',
					['pterygiumRight.new_contentid']: 'NR0163',
					['pterygiumRight.new_result']: 'SC0000000029',

					//翼状胬肉(左眼)
					['pterygiumLeft.new_categoryid']: 'LB0002',
					['pterygiumLeft.new_itemid']: 'XM0003',
					['pterygiumLeft.new_contentid']: 'NR0164',
					['pterygiumLeft.new_result']: 'SC0000000029',

					//泪道阻塞
					['lacrimalObstruction.new_categoryid']: 'LB0003',
					['lacrimalObstruction.new_itemid']: 'XM0004',
					['lacrimalObstruction.new_contentid']: 'NR0064',
					['lacrimalObstruction.new_result']: 'SC0000000029',
					//倒睫
					['trichiasis.new_categoryid']: 'LB0010',
					['trichiasis.new_itemid']: 'XM0029',
					['trichiasis.new_contentid']: 'NR0043',
					['trichiasis.new_result']: 'SC0000000029',
					//结膜炎
					['conjunctivitis.new_categoryid']: 'LB0002',
					['conjunctivitis.new_itemid']: 'XM0003',
					['conjunctivitis.new_contentid']: 'NR0155',
					['conjunctivitis.new_result']: 'SC0000000029',
					//屈光不正
					['ametropia.new_categoryid']: 'LB0005',
					['ametropia.new_itemid']: 'XM0055',
					['ametropia.new_contentid']: 'NR0156',
					['ametropia.new_result']: 'SC0000000029',
					//眼底疾病
					['fundusDiseases.new_categoryid']: 'LB0010',
					['fundusDiseases.new_itemid']: 'XM0029',
					['fundusDiseases.new_contentid']: 'NR0157',
					['fundusDiseases.new_result']: 'SC0000000029',
					//其他描述
					['descriptions.new_categoryid']: 'LB0012',
					['descriptions.new_itemid']: 'XM0056',
					['descriptions.new_contentid']: 'NR0158',
					['descriptions.new_result']: '',

					//建议
					['advise.new_categoryid']: 'LB0012',
					['advise.new_itemid']: 'XM0046',
					['advise.new_contentid']: 'NR0142',
					['advise.new_result']: '',
          
				})
				var list = this.data.screenitem;
				var screenitemIndex =this.getPickerValue(list,'new_screenitemId',this.data.screenitemId);
				if(screenitemIndex!=undefined||screenitemIndex!=null)
				{
					var new_screenitemId = list[screenitemIndex].new_screenitemId;			
					this.setData({
						screenitemIndex: screenitemIndex,
						['leadEntity.new_screenitemId'] : new_screenitemId,
						['sccustomer.new_scitemid']: new_screenitemId,
						['reservation.new_new_screenitem']:new_screenitemId,
					});
				}	
				var insuranceTypeIndex = this.data.insuranceTypeIndex;
				if (insuranceTypeIndex != 0) {
					var new_insurancetype = this.data.insuranceType[insuranceTypeIndex].id;
					this.setData({
						['reservation.new_insurancetype']: new_insurancetype,
						['leadEntity.new_insurancetype'] : new_insurancetype,
					})
				}
				var diseaseIndex = this.data.diseaseIndex;				
				if (diseaseIndex != 0) {
					var new_disease = this.data.diseaseArray[diseaseIndex].id
					this.setData({
						['reservation.new_contnt']: new_disease,
						['leadEntity.new_disease'] : new_disease,
					})
				}
			}
		},
		//判断字符串是否为数字
		checkNum: function (inputNum) {
			var re = /^[0-9]+.?[0-9]*$/;
			if (!re.test(inputNum)) {
				return false;
			}
			else
			{
				return true;
			}
		},
		//判断是否为手机
		checkPhone:function(phone){
			var regPhoneNum = /^1(3|4|5|6|7|8|9)\d{9}$/;
			if (!regPhoneNum.test(phone)) {
				return false;
			}
			else {
				return true;
			}
		},
		//提示
		shouTips:function(){
			wx.showToast({
				title: '请输入数字',
				icon: 'none',
				duration: 2000
			});
		}
	}
});